        $(function () { 

            $("#apsgrid").datagrid({
            	title: "",
                url : basePath+"approvalProcess/list",
                width: "100%",  
                height: "100%",  
                singleSelect : true, /*是否选中一行*/
                width:'auto', 
                pagination : true,/*是否显示下面的分页菜单*/
                border:true,
                nowrap:false,
                rowStyler: function() {
                	　　return 'height: 44px';
                	},
                    onLoadSuccess:function(data){  
                    },
                //rownumbers:true,
                columns : [ [  {field : 'id', title : 'ID',width : '250',align : 'center',hidden:'true'}, 
                			   {field : 'name', title : '名称',width : '300',align : 'center',
				                	formatter:function(value,row,index){
				                		return "<a class='fonta' href='#' onclick=toApprovalProcessDetail("+row.type+");>"+value+"</a>";
				 				  
				                	}            
                			   },
                			   
                			   
                			   {field : 'type', title : '审核流程类型',width : '300',align : 'center',
                				   formatter:function(value,row,index){
                                	   if(value==1){
                                		   return "合同";
                                	   }else if(value==2){
                                		   return "项目";
                                	   }else if(value==3){
                                		   return "销售机会";                                	   
                                	   }else{
                                		    return "";                      	   
                                	   }
                				   }
                			   },
  
                			   {field : 'createTime', title : '创建时间',width : '180',align : 'center'},
                               {field : 'op',title : '&nbsp;&nbsp;操作',width : '180',align : 'center',formatter:function(value,row,index){
                            	   return "<a class='fonta' href='#' onclick=toUpdateApprovalProcess("+row.type+");>修改</a>&nbsp;&nbsp;|" +
                            		   		"&nbsp;&nbsp;<a class='fonta' href='#' onclick=toDelete("+row.type+");>删除</a>";
                            	   
                               	}
                               }
                			   
                               
                ]],
            	loadMsg : '数据加载中,请稍候......'
            }); 
  
            var p = $('#apsgrid').datagrid('getPager');  
            $(p).pagination({  
                /*  
                    页数文本框前显示的汉字 修改每页默认条数   
                    搜索pageList在jquery.easyui.min.js中修改，  
                    分页区下拉分页数量集合和默认每页分页条数  
                    striped属性 交替行换色  
                */  
                beforePageText: '第',  
                afterPageText: '页    共 {pages} 页',  
                displayMsg: '当前显示 {from}-{to} 条记录,共 {total} 条记录'  
            });
            
        });
        
        

        function toAddApprovalProcess(){
            $('#toAddApprovalProcessDlg').dialog({
                title: "新增",
                width: 1000,
                height: 550,
                closed: false,
                cache: false,
                onClose:function(){
                	$('#apsgrid').datagrid('clearSelections');
                },
                href: basePath+'approvalProcess/navigator?action=add',
                onLoad:function(){
                	loadType();
                },
                modal: true
            });        	
        }

        function toUpdateApprovalProcess(type){
        	//这个Type是审核类型的TYPE
            $('#toAddApprovalProcessDlg').dialog({
                title: "修改",
                width: 1000,
                height: 550,
                closed: false,
                cache: false,
                onClose:function(){
                	$('#apsgrid').datagrid('clearSelections');
                },
                href: basePath+'approvalProcess/navigator?action=update&type='+type,
                onLoad:function(){
                	loadType();
                	reloaduserDiv(1,$("#approvaluuid").val());
                	reloaduserDiv(2,$("#copytouuid").val());
                },
                modal: true
            });        	
        }
        
        function toApprovalProcessDetail(type){
        	//这个Type是审核类型的TYPE
            $('#toAddApprovalProcessDlg').dialog({
                title: "流程",
                width: 1000,
                height: 550,
                closed: false,
                cache: false,
                onClose:function(){
                	$('#apsgrid').datagrid('clearSelections');
                },
                href: basePath+'approvalProcess/navigator?action=detail&type='+type,
                onLoad:function(){
                	loadType();
                	reloaduserDiv(1,$("#approvaluuid").val());
                	reloaduserDiv(2,$("#copytouuid").val());
                },
                modal: true
            });         	
        }
        
        function toDelete(type){
        	$.messager.confirm('提示框', '你确定要删除吗?',function(r){
        		if(r){
                    $.ajax({
                        type:"post",
                        url:basePath+"approvalProcess/deleteProcess",
                        data : {
                        	type:type
                        },
                        cache:false,
                        dataType: "json", //this is important
                        success:function(result) {
                	    	if(result.result=="success"){   
                	    		$('#apsgrid').datagrid('reload');//刷新
                             	$.messager.show({
                             		title:'提示',
                             		msg:'删除成功',
                             		timeout:5000,
                             		showType:'slide'
                             	});        	    		
                	    	}
                        }
                    });        			
        		}
        	});
        	
        }

        function loadType(){
        	if($('#action').val()!='add'){
        		$("#addupdate #approvalTypeDiv").css("display","none");
        	}
        	if($('#action').val()=='detail'){
        		$("#submitbutton").hide();
        	}
            $('#addupdate #approvalType').combobox({
        	    //disabled:true,
        	    url:basePath+'tree/loadApprovalType',
        	            valueField:'id',
        	            textField:'text',
        	            editable:false,
        	            onClick:function(record){

        	            },
                	    onLoadSuccess:function(){ //默认选中第一条数据
                	    	var data= $('#addupdate #approvalType').combobox("getData");
                    	                if (data.length > 0) {
                    	                	$('#addupdate #approvalType').combobox('select', data[0].id);
                    	                }
                	            	}       
            }); 
        }
        
        function toSelectRole(){
            $('#toSelectUserDlg').dialog({
                title: "选择角色",
                width: 200,
                height: 400,
                closed: false,
                cache: false,
                onClose:function(){
                	
                },
                href: basePath+'/navigator?page=systemconfig/approvalProcess/toSelectRole',
                onLoad:function(){
                	var rolecombotree = $("#roleTree");
                	rolecombotree.combotree({
                			url: basePath+'role/loadRoleCombobox',
                	         cascadeCheck:false, 
                             valueField: 'id',  
                             textField: 'text',  
                             editable: false ,
                	         prompt: '请选择...',
                	         onlyLeafCheck:true,
                	         onSelect:function(node) {
                	        	 addRole(node.id,node.text);
                	         },
                	         onLoadSuccess:function(){
       	        	 
                	         },
                	         onBeforeExpand: function (node) {
                	         }
                	     }); 
                	
                },
                modal: true
            });        	
        }
        
        function addRole(roleId,roleName){
        	var uuid = $("#approvaluuid").val();
        	if(uuid!=''){
            	$.ajax({  
            		type: 'post',
            		async: false,
            		dataType: "json",
            		url:basePath+'approvalProcess/addRoles',
            		data:{
            			roleId:roleId,
            			roleName:roleName,
            			uuid:uuid
            		},
            		success: function(data){
            			//alert(data.result);
            			if(data.result=='success'){
            				reloaduserDiv(1,uuid);
            			}
            			
            		},
            		error:function(){}
            	});       		
        	}
        	
        }

        function toSelectUsers(type){
            $('#toSelectUserDlg').dialog({
                title: "选择人员",
                width: 300,
                height: 300,
                closed: false,
                cache: false,
                onClose:function(){
                	
                },
                href: basePath+'/navigator?page=systemconfig/approvalProcess/toSelectUser',
                onLoad:function(){
                	loadDept(type);
                	loadDeptUser();
                },
                modal: true
            });        	
        }
        
        function loadDept(type){
        	//初始化COMBOTREE-部门
        	var deptcombotree = $("#deptuser #deptComboTree");
        	deptcombotree.combotree({
        	         url: basePath+"dept/loadDeptComboTree",//初始combotree
        	         cascadeCheck:false, 
                     valueField: 'id',  
                     textField: 'text',  
                     editable: false ,
        	         prompt: '请选择一个部门...',
        	         onlyLeafCheck:true,
        	         onSelect:function(node) {
        	        	 loadDeptUser(type,node.id);
        	         },
        	         onLoadSuccess:function(){
        	        	 
        	         },
        	         onBeforeExpand: function (node) {
        	         }
        	     });        	
        }
     


        function loadDeptUser(type,deptId){
        	var usercombotree = $("#deptuser #userComboTree");
        	usercombotree.combotree({
        	         url: basePath+"tree/loadUserParent?deptId="+deptId+"&qry=all",
        	         cascadeCheck:false, 
                     valueField: 'id',  
                     textField: 'text',  
                     editable: false ,
        	         prompt: '请选择...',
        	         onlyLeafCheck:true,
        	         onSelect:function(node) {
        	        	 addApprovalCopyto(type,node.id,node.text);
        	         },
        	         onLoadSuccess:function(){
//             	    	var data = $('#userComboTree').combobox("getData");
//    	                if (data.length > 0) {
//    	                	$('#userComboTree').combobox('select', data[0].id);
//    	                }        	        	 
        	         },
        	         onBeforeExpand: function (node) {
        	         }
        	     });         	
        }

        function addApprovalCopyto(type,userId,userName){
        	var uuid = "";
        	if(type==1){
        		uuid=$("#approvaluuid").val()
        	}else{
        		uuid=$("#copytouuid").val()
        	}

        	if(uuid!=''){
            	$.ajax({  
            		type: 'post',
            		async: false,
            		dataType: "json",
            		url:basePath+'approvalProcess/addUsers',
            		data:{
            			type:type,
            			userId:userId,
            			userName:userName,
            			uuid:uuid
            		},
            		success: function(data){
            			//alert(data.result);
            			if(data.result=='success'){
            				reloaduserDiv(type,uuid);
            			}
            			
            		},
            		error:function(){}
            	});       		
        	}
        	
        }
        

        function reloaduserDiv(type,uuid){
        	$.ajax({  
        		type: 'get',
        		//async: false,
        		dataType: "json",
        		//url:basePath+'tree/loadTaskParticipators',
        		url:basePath+'approvalProcess/loadUsers',
        		data:{
        			type:type,
        			uuid:uuid
        		},
        		success: function(data){
        			var a = "";
        			if(type==2){
            			$.each(data,function(i,n){	
            				a += "<input type='button' title='"+n.userName+"' class='circleuser' value='"+n.userName+"' /><a href='#' onclick=deleteUser("+type+","+n.userId+");>X</a>&nbsp;&nbsp;";
            			});        				
        				$("#copyToUsers").html(a);
        			}else{
            			$.each(data,function(i,n){	
            				a += "<input type='button' title='"+n.roleName+"' class='circleuser' value='"+n.roleName+"' /><a href='#' onclick=deleteUser("+type+","+n.roleId+");>X</a>&nbsp;&nbsp;";
            				
            			});        				
        				$("#approvalUsers").html(a);
        			}
        		}
        	});        	
        }

        function deleteUser(type,userId){
        	debugger;
        	var uuid = "";
        	if(type==1){
        		uuid=$("#approvaluuid").val()
        	}else{
        		uuid=$("#copytouuid").val()
        	}
        	if(uuid != ''){
            	$.ajax({  
            		type: 'post',
            		//async: false,
            		dataType: "json",
            		url:basePath+'approvalProcess/deleteUser',
            		data:{
            			type:type,
            			uuid:uuid,
            			userId:userId
            		},
            		success: function(data){
            			reloaduserDiv(type,uuid);		
            		},
            		error:function(){}
            	});        		
        	}
        	
        }
        

        function toSubmit(){
        	var approvalType = $('#approvalType').combobox('getValue');
        	$.ajax({  
        		type: 'post',
        		//async: false,
        		dataType: "json",
        		url:basePath+'approvalProcess/submitApprovalCopytoUser',
        		data:{
        			action:$("#action").val(),
        			approvalType : approvalType,
        			approvaluuid:$("#approvaluuid").val(),
        			copytouuid:$("#copytouuid").val()
        		},
        		success: function(data){
        			if(data.result=='success'){
        				$('#apsgrid').datagrid('reload');//刷新
        				cancelDlg();
                     	$.messager.show({
                     		title:'提示',
                     		msg:'操作成功',
                     		timeout:5000,
                     		showType:'slide'
                     	});
        			}else{
        				$.messager.alert('错误提示',data.result,'warning');
        			}
        		},
        		error:function(){}
        	});        	
        }

        
        function cancelDlg(){
        	$("#toAddApprovalProcessDlg").dialog("close");
        }
        

        


        
